package com.sde.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Author SDE
 * @Classname: ChatController
 * @Date 2025-07-02 16:11
 * @Package com.sde.controller
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/common/ai")
public class ChatController {

    private final ChatClient chatClient;


    /**
     * 同步调用
     * @param prompt
     * @return
     */
    @GetMapping("/chat")
    public String chant(@RequestParam(defaultValue = "你是谁？") String prompt){
        return chatClient
                .prompt( prompt) //传入user提示词
                .call()//同步请求，会等ai全部输出完才返回结果
                .content();//返回响应内容
    }

    /**
     * 流式调用
     * @param prompt
     * @return
     */
    @GetMapping(value = "/stream",produces = "text/html;charset=UTF-8")
    public Flux<String> streamChat(@RequestParam(defaultValue = "你是谁？") String prompt){
        return chatClient
                .prompt( prompt)
                .stream() //流式调用
                .content();
    }
}
